/**
 * 头像工具函数
 */

// 默认头像URL
export const DEFAULT_AVATAR_URL = 'https://api.dicebear.com/7.x/avataaars/svg?seed=user&backgroundColor=b6e3f4&mouth=smile';

/**
 * 获取用户头像URL
 * @param {string} userAvatar - 用户头像URL
 * @returns {string} 头像URL
 */
export function getUserAvatarUrl(userAvatar) {
  if (userAvatar && userAvatar.trim() !== '') {
    return userAvatar;
  }
  return DEFAULT_AVATAR_URL;
}

/**
 * 处理头像加载失败
 * @param {Event} event - 错误事件
 */
export function handleAvatarError(event) {
  event.target.src = DEFAULT_AVATAR_URL;
}

/**
 * 生成基于用户名的默认头像
 * @param {string} username - 用户名
 * @returns {string} 默认头像URL
 */
export function generateDefaultAvatar(username) {
  if (!username) {
    return DEFAULT_AVATAR_URL;
  }
  
  // 使用用户名作为种子生成唯一的默认头像
  const seed = username.toLowerCase().replace(/[^a-z0-9]/g, '');
  return `https://api.dicebear.com/7.x/avataaars/svg?seed=${seed}&backgroundColor=b6e3f4&mouth=smile`;
}